package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.CommunityPostInfoAdpater;
import cn.otrue.patienthealthmanager.adapter.MainDataInfoMainDataInfoActivityAdpater;
import cn.otrue.patienthealthmanager.bean.ThreadModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.DensityUtil;
import cn.otrue.patienthealthmanager.utils.DialogUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 健康社区->帖子详情
 * 
 * @author G
 * 
 */
public class CommunityPostInfoActivity extends BaseActivity {
	private List<ThreadModel> good;
	private ThreadModel goods;
	private TextView tw_huifu;

	private TextView tv_xiangqing;
	private TextView tv_neirong;
	private TextView tv_title_title;
	private int threadId;
	private AlertDialog alertDialog;
	private CommunityPostInfoAdpater Adapter;

	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.showImageForEmptyUri(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(
								CommunityPostInfoActivity.this, 40))).build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_communtiy);

		initView();
	}

	private void initView() {
		tv_uuuu_mingzi = (TextView) findViewById(R.id.tv_uuuu_mingzi);
		good = new ArrayList<ThreadModel>();
		rou = (LinearLayout) findViewById(R.id.rou);
		pe_head_image = (ImageView) findViewById(R.id.pe_head_image);
		tv_zan = (TextView) findViewById(R.id.tv_zan);
		iv_zan = (ImageView) findViewById(R.id.iv_zan);

		iv_zan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (MyConst.getACCOUNT().equals("")) {
					View dialogView = initDataInputDialog(
							new DataDialogListener(), 0);
					if (null != dialogView)
						alertDialog = DialogUtil.showDialog(
								CommunityPostInfoActivity.this, dialogView,
								true);
				} else {
					iv_zan.setOnClickListener(CommunityPostInfoActivity.this);
					iv_zan.setClickable(false);
					getdianzan();
					iv_zan.setImageResource(R.drawable.zanzanznanzan);
					iv_zan.setTag("1");
				}

			}
		});
		tv_community_title = (TextView) findViewById(R.id.tv_community_title);
		tv_community_name = (TextView) findViewById(R.id.tv_community_name);
		tv_community_time = (TextView) findViewById(R.id.tv_community_time);
		tv_community_num = (TextView) findViewById(R.id.tv_community_num);
		iv_lishitupian = (ListView) findViewById(R.id.iv_lishitupian);
		tw_huifu = (TextView) findViewById(R.id.tw_huifu);
		tw_huifu.setOnClickListener(this);
		nima = (RelativeLayout) findViewById(R.id.nima);
		nima.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("threadId", threadId);
				intent.setClass(CommunityPostInfoActivity.this,
						HuiFUActivity.class);
				startActivity(intent);

			}
		});
		tv_shijian = (TextView) findViewById(R.id.tv_shijian);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_num = (TextView) findViewById(R.id.tv_num);
		tv_huifu = (TextView) findViewById(R.id.tv_huifu);

		et_huifu = (EditText) findViewById(R.id.et_huifu);
		et_huifu.setOnClickListener(this);

		tv_xiangqing = (TextView) findViewById(R.id.tv_xiangqing);
		tv_neirong = (TextView) findViewById(R.id.tv_neirong);
		getData();
		initTitleBar();
		getdian();
	}

	private void getData() {
		threadId = getIntent().getIntExtra("threadId", threadId);
		getxiangqing();
		getfatie();
	}

	private Handler fatieHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {

			if (state.equals("001")) {
				String tuu = data.getString(HttpCons.DATA);

				List<ThreadModel> iiii = HttpServer.gson.fromJson(tuu,
						new TypeToken<List<ThreadModel>>() {
						}.getType());
				if (iiii.size() != 0) {

					rou.setVisibility(View.VISIBLE);
					String url = iiii.get(0).getHeadimg();
					ImageSet(url, pe_head_image);

					tv_community_name.setText(iiii.get(0).getReplyContent());
					tv_uuuu_mingzi.setText(iiii.get(0).getNickName());
					// long num = Long.parseLong(iiii.get(0).getCreatetime()) ;
					// Date uuu = new Date(num);
					//
					// String dateStr = CommonUtil.getFormatDate("yyyy-MM-dd",
					// uuu);
					tv_community_time.setText(iiii.get(0).getCreatetime()
							.toString().substring(0, 10));

				}

				Toast.makeText(CommunityPostInfoActivity.this, "操作成功", 0)
						.show();
			} else {
				Toast.makeText(CommunityPostInfoActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(CommunityPostInfoActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private void getfatie() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("threadId", threadId);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword(" ");
		parameter.setCurrentPage("1");
		parameter.setPagesize("20");
		parameter.setData(map);
		new HttpServer().post(HttpCons.answeredList, parameter, fatieHandler);

	}

	private Handler dianzanHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String dataStr = data.getString(HttpCons.DATA);
			if (state.equals("001")) {

				getxiangqing();
				Toast.makeText(CommunityPostInfoActivity.this, msg, 0).show();

			} else {
				Toast.makeText(CommunityPostInfoActivity.this, msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			iv_zan.setOnClickListener(CommunityPostInfoActivity.this);
			iv_zan.setClickable(true);
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(CommunityPostInfoActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private Handler shifoHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			String dataStr = data.getString(HttpCons.DATA);
			if (state.equals("001")) {
				if (dataStr.equals("true")) {
					iv_zan.setOnClickListener(CommunityPostInfoActivity.this);
					iv_zan.setClickable(false);
					iv_zan.setImageResource(R.drawable.zanzanznanzan);
				}

				Toast.makeText(CommunityPostInfoActivity.this, msg, 0).show();

			} else {
				Toast.makeText(CommunityPostInfoActivity.this, msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			iv_zan.setOnClickListener(CommunityPostInfoActivity.this);
			iv_zan.setClickable(true);
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(CommunityPostInfoActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private void getdian() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("threadId", threadId);
		map.put("useraccount", MyConst.getACCOUNT());
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword(" ");
		parameter.setChannelId("otrue_android_patient_channel");
		parameter.setCurrentPage("");
		parameter.setPagesize("");
		parameter.setData(map);
		new HttpServer().post(HttpCons.InformationQueryPraise, parameter,
				shifoHandler);

	}

	private void getdianzan() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("threadId", threadId);
		map.put("someGreatState", 0);
		map.put("useraccount", MyConst.getACCOUNT());
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword(" ");
		parameter.setCurrentPage("");
		parameter.setPagesize("");
		parameter.setData(map);
		new HttpServer().post(HttpCons.addPraiseNumber, parameter,
				dianzanHandler);

	}

	private void initTitleBar() {
		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.loginii));

		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});

	}

	private Handler communityHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {

			String Str = data.getString(HttpCons.DATA);
			Log.e("AAAA", Str);
			if (state.equals("001")) {
				Gson gson = HttpServer.gson;
				goods = gson.fromJson(Str, ThreadModel.class);

				tv_huifu.setText("(" + goods.getReplyCount() + ")");
				tv_name.setText(goods.getNickName());
				tv_shijian.setText(goods.getCreatetime().toString()
						.substring(0, 10));
				tv_num.setText("(" + goods.getPraiseCount() + ")");
				tv_xiangqing.setText(goods.getThreadTitle());
				tv_neirong.setText(goods.getThreadContent());

				List<Map<String, String>> imagelist = (List<Map<String, String>>) goods
						.getImglist();

				MainDataInfoMainDataInfoActivityAdpater postAdapter = new MainDataInfoMainDataInfoActivityAdpater(
						imagelist, CommunityPostInfoActivity.this);
				iv_lishitupian.setAdapter(postAdapter);
			} else {
				Toast.makeText(getApplicationContext(), msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(CommunityPostInfoActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private EditText et_huifu;

	private void getxiangqing() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (threadId != 0) {
			map.put("threadId", threadId);

			TransferParameter parameter = new TransferParameter();
			parameter.setUsername(MyConst.getACCOUNT());
			parameter.setPassword(" ");
			parameter.setData(map);
			new HttpServer().post(HttpCons.query_Thread_Detail, parameter,
					communityHandler);
		} else {
			Toast.makeText(CommunityPostInfoActivity.this, "暂无数据",
					Toast.LENGTH_SHORT).show();

		}

	}

	private Handler huifuHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {

			String Str = data.getString(HttpCons.DATA);
			Log.e("AAAAAA", Str);
			if (state.equals("001")) {
				et_huifu.setText("");
				getxiangqing();
				getfatie();
				 InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				 imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
				Toast.makeText(getApplicationContext(), "回复成功", 0).show();
			} else {
				Toast.makeText(getApplicationContext(), "回复失败", 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {

			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(CommunityPostInfoActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	private TextView tv_shijian;
	private TextView tv_name;
	private TextView tv_num;
	private TextView tv_huifu;
	private ListView iv_lishitupian;
	private TextView tv_zan;
	private TextView tv_community_title;
	private TextView tv_community_name;
	private TextView tv_community_time;
	private TextView tv_community_num;
	private ImageView pe_head_image;
	private RelativeLayout nima;
	private LinearLayout rou;
	private ImageView iv_zan;
	private TextView tv_uuuu_mingzi;

	private View initDataInputDialog(DataDialogListener dataListener, int i) {
		LayoutInflater inflater = LayoutInflater
				.from(CommunityPostInfoActivity.this);
		View view = inflater.inflate(R.layout.dialog_input_daaa, null);
		TextView tv_catt_dialogLeft = (TextView) view
				.findViewById(R.id.tv_catt_dialogLeft);
		tv_catt_dialogLeft.setOnClickListener(dataListener);
		TextView tv_catt_dialogRight = (TextView) view
				.findViewById(R.id.tv_catt_dialogRight);
		tv_catt_dialogRight.setOnClickListener(dataListener);
		return view;

	}

	public class DataDialogListener implements OnClickListener {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.tv_catt_dialogLeft:// 取消
				alertDialog.dismiss();
				break;
			case R.id.tv_catt_dialogRight:// 确定

				Intent personinfoIntent = new Intent(
						CommunityPostInfoActivity.this, LoginActivity.class);
				startActivity(personinfoIntent);

			}
		}

	}

	private void gethuifu() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (threadId != 0) {
			map.put("threadId", threadId);
		}
		if (et_huifu.getText().toString().equals("")) {
			Toast.makeText(CommunityPostInfoActivity.this, "回复不能为空",
					Toast.LENGTH_SHORT).show();
		} else {
			map.put("replyContent", et_huifu.getText().toString());

			TransferParameter parameter = new TransferParameter();
			parameter.setPassword("");
			parameter.setData(map);
			new HttpServer().post(HttpCons.insert_Thread_Reply, parameter,
					huifuHandler);
		}

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tw_huifu:
			if (MyConst.getACCOUNT().equals("")) {
				View dialogView = initDataInputDialog(new DataDialogListener(),
						0);
				if (null != dialogView)
					alertDialog = DialogUtil.showDialog(
							CommunityPostInfoActivity.this, dialogView, true);
			} else {
				gethuifu();

			}

			break;

		default:
			break;
		}

	}

}
